<%-- 
    Document   : find
    Created on : 14.05.2011, 14:04:00
    Author     : Mikhail
--%>

<%@page import="unc.pds.model.GroupRemote"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="unc.pds.service.ServiceBeanRemoteHome"%>
<%@page import="unc.pds.service.ServiceBeanRemote"%>
<%@page import="unc.pds.filemodel.PhotoRemote"%>
<%@page import="unc.pds.filemodel.AlbumRemote"%>
<%@page import="unc.pds.filemodel.FileRemote"%>
<%@page import="unc.pds.filemodel.FolderRemote"%>
<%@page import="unc.pds.model.DiaryRemote"%>
<%@page import="unc.pds.model.UserRemote"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="unc.pds.data.Consts"%>
<%@page import="unc.pds.seen.PDSSerchEngine"%>
<%@page import="unc.pds.seen.SearchEngineFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="resources/stylesheet.css" type="text/css"/>
        <title>Мой дневник</title>
    </head>
    <body>
        <%
            ServiceBeanRemote service = ((ServiceBeanRemoteHome)(new InitialContext()).lookup("ejb/ServiceBean")).create((Long)session.getAttribute("sid"));
            PDSSerchEngine search = SearchEngineFactory.getEngine();
            String type;
            String string;
            if (request.getParameter("type")!=null) {
                type = request.getParameter("type");
            } else {
                type = Consts.OBJECT_TYPE__USER;
            }
            if (request.getParameter("string")!=null) {
                string = request.getParameter("string");
            } else {
                string = "";
            }
            Collection result;
        %>
    <div id="container">
        <div id="header">
            <% if(session.getAttribute("sid")!=null) { %>
            Вы вошли как <a href ="index.jsp?id=<%= service.getUser().getKey() %>&view=posts">
                <%= service.getUser().getNickname() %>
            </a><br/>
            <a href = "logout-processor.jsp">Выход</a>
            <% } %>
        </div>
        <div id="facet">
            <form action="find.jsp" method="get" onsubmit="return validateEmpty('string');">
                <p><input type="text" name="string" id="string" value="<%=string%>"/></p>
                <p>
                    <input type="radio" name="type" value="<%=Consts.OBJECT_TYPE__USER%>" <%=type.equals(Consts.OBJECT_TYPE__USER)?"checked":""%>/>
                    Пользователи
                    <input type="radio" name="type" value="<%=Consts.OBJECT_TYPE__GROUP%>" <%=type.equals(Consts.OBJECT_TYPE__GROUP)?"checked":""%>/>
                    Группы
                    <input type="radio" name="type" value="<%=Consts.OBJECT_TYPE__DIARY%>" <%=type.equals(Consts.OBJECT_TYPE__DIARY)?"checked":""%>/>
                    Записи
                    <input type="radio" name="type" value="<%=Consts.OBJECT_TYPE__FOLDER%>" <%=type.equals(Consts.OBJECT_TYPE__FOLDER)?"checked":""%>/>
                    Папки
                    <input type="radio" name="type" value="<%=Consts.OBJECT_TYPE__FILE%>" <%=type.equals(Consts.OBJECT_TYPE__FILE)?"checked":""%>/>
                    Файлы
                    <input type="radio" name="type" value="<%=Consts.OBJECT_TYPE__ALBUM%>" <%=type.equals(Consts.OBJECT_TYPE__ALBUM)?"checked":""%>/>
                    Альбомы
                    <input type="radio" name="type" value="<%=Consts.OBJECT_TYPE__PHOTO%>" <%=type.equals(Consts.OBJECT_TYPE__PHOTO)?"checked":""%>/>
                    Фотографии
                </p>
                <p><input type="submit" value="Искать"/></p>
            </form>
            <%
            if (type.equals(Consts.OBJECT_TYPE__USER)) {
                result = search.getByField(type, Consts.ATTR__USER_NICKNAME, string);
                Iterator iter = result.iterator();
                while (iter.hasNext()) {
                    UserRemote current = (UserRemote)service.getByKey((Long)iter.next());
            %>
                    <p><a href="index.jsp?id=<%=current.getKey()%>&view=posts"><%=current.getNickname()%></a></p>
            <%
                }
            } else if (type.equals(Consts.OBJECT_TYPE__GROUP)) {
                result = search.getByField(type, Consts.ATTR__USER_NICKNAME, string);
                Iterator iter = result.iterator();
                while (iter.hasNext()) {
                    GroupRemote current = (GroupRemote)service.getByKey((Long)iter.next());
            %>
                    <p><a href="index.jsp?id=<%=current.getKey()%>&view=posts"><%=current.getName()%></a></p>
            <%
                }
            } else if (type.equals(Consts.OBJECT_TYPE__DIARY)) {
                result = search.getByField(type, Consts.ATTR__DIARY_CAPTION, string);
                Iterator iter = result.iterator();
                while (iter.hasNext()) {
                    DiaryRemote current = (DiaryRemote)service.getByKey((Long)iter.next());
            %>
                    <p><a href="index.jsp?id=<%=current.getKey()%>"><%=current.getCaption()%></a> - <a href="index.jsp?id=<%=current.getAuthor().getKey()%>&view=posts"><%=current.getAuthor().getNickname()%></a></p>
            <%
                }
            } else if (type.equals(Consts.OBJECT_TYPE__FOLDER)) {
                result = search.getByField(type, Consts.ATTR__FOLDER_NAME, string);
                Iterator iter = result.iterator();
                while (iter.hasNext()) {
                    FolderRemote current = (FolderRemote)service.getByKey((Long)iter.next());
            %>
                    <p><a href="index.jsp?id=<%=current.getKey()%>"><%=current.getName()%></a> - <a href="index.jsp?id=<%=current.getAuthor().getKey()%>&view=posts"><%=current.getAuthor().getNickname()%></a></p>
            <%
                }
            } else if (type.equals(Consts.OBJECT_TYPE__FILE)) {
                result = search.getByField(type, Consts.ATTR__FILE_NAME, string);
                Iterator iter = result.iterator();
                while (iter.hasNext()) {
                    FileRemote current = (FileRemote)service.getByKey((Long)iter.next());
            %>
                    <p><a href="index.jsp?id=<%=current.getKey()%>"><%=current.getName()%></a> - <a href="index.jsp?id=<%=current.getAuthor().getKey()%>&view=posts"><%=current.getAuthor().getNickname()%></a></p>
            <%
                }
            } else if (type.equals(Consts.OBJECT_TYPE__ALBUM)) {
                result = search.getByField(type, Consts.ATTR__ALBUM_NAME, string);
                Iterator iter = result.iterator();
                while (iter.hasNext()) {
                    AlbumRemote current = (AlbumRemote)service.getByKey((Long)iter.next());
            %>
                    <p><a href="index.jsp?id=<%=current.getKey()%>"><%=current.getName()%></a> - <a href="index.jsp?id=<%=current.getAuthor().getKey()%>&view=posts"><%=current.getAuthor().getNickname()%></a></p>
            <%
                }
            } else if (type.equals(Consts.OBJECT_TYPE__PHOTO)) {
                result = search.getByField(type, Consts.ATTR__PHOTO_NAME, string);
                Iterator iter = result.iterator();
                while (iter.hasNext()) {
                    PhotoRemote current = (PhotoRemote)service.getByKey((Long)iter.next());
            %>
                    <p><a href="index.jsp?id=<%=current.getKey()%>"><%=current.getName()%> - <%=current.getAuthor().getNickname()%></a></p>
            <%
                }
            }
            %>
        </div>
    </div>
    </body>
</html>
